package logic

import (
	"context"
	"errors"
	"jinrigaoping/rpc/sys/internal/svc"
	"jinrigaoping/rpc/sys/sys"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type SysUserByAccountLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSysUserByAccountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SysUserByAccountLogic {
	return &SysUserByAccountLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SysUserByAccountLogic) SysUserByAccount(in *sys.SysUserByAccountReq) (*sys.SysUserByAccountResp, error) {

	sysUser, err := l.svcCtx.SysUserModel.FindOneByAccount(l.ctx, in.Account)
	if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
		logx.Errorf("FindOneByAccount account: %s error: %v", in.Account, err)
		return nil, err
	}

	if sysUser == nil {
		return nil, nil
	}

	return &sys.SysUserByAccountResp{
		Id:       sysUser.Id,
		Account:  sysUser.Account,
		Password: sysUser.Password,
		Status:   sysUser.Status,
		RoleId:   sysUser.RoleId,
		UserType: int32(sysUser.UserType),
	}, nil
}
